<template>
  <el-dialog
    title="分配角色"
    :visible="showRoleDialog"
    @close="handleClose"
  >
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group>
      <!-- 选项 -->
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="updateAssignRole">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getUserDetail } from '@/api/user'
import { getRoleList } from '@/api/setting'
import { empAssignRole } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleList: [],
      roleIds: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
      //   console.log(this.roleList)
    },
    async getUserDetail(id) {
      const { roleIds } = await getUserDetail(id)
      this.roleIds = roleIds
    //   console.log(this.roleIds)
    },
    handleClose() {
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    },
    async updateAssignRole() {
      const roleIds = this.roleIds
      const id = this.userId
      console.log({ id, roleIds })
      await empAssignRole({ id, roleIds })
      this.$message.success('分配角色权限成功')
      this.handleClose()
    }
  }
}
</script>

<style scoped>
</style>
